<!--#Include File="Include.asp"--><%
'--------------------------------------
'Power By 方卡在线 @2010-2014 f02
'URL:http://www.fangka.net/
'EMail:admin@ifangka.com
'QQ:861118936
'版权所有 未经允许不得去除本版权信息
'--------------------------------------

Call CheckAdminType("0,1",UType)
Call CheckAdminLimit(ULimit,"s_Template",0)

Dim NowFile,NowFloder,DirFloder,readFloder
Dim Fk_Template_Name,Fk_Template_Content

Types=Clng(Request.QueryString("Type"))

Select Case Types
	Case 1
		Call TemplateList() '模板列表
	Case 2
		Call TemplateAddForm() '添加新文件表单
	Case 3
		Call TemplateAddDo() '添加新文件执行
	Case 4
		Call TemplateEditForm() '修改文件表单
	Case 5
		Call TemplateEditDo() '修改文件执行
	Case 6
		Call TemplateReLoad() '模板缓存重新载入
	Case 7
		Call TemplateTempList() '模板缓存列表
	Case 8
		Call TemplateTempDel() '模板缓存删除执行
	Case Else
		Call FKFun.ShowErr("未发现功能项！",1)
End Select

'==========================================
'函 数 名：TemplateList()
'作    用：模板列表
'==========================================
Sub TemplateList()
	NowFloder=FKFun.HTMLEncode(Trim(Request.QueryString("NowFloder")))
	
	Response.Cookies("url_f02")=FKFun.GetNowUrl()
%>
		<div id="rightNav">
			<ul>
<%
If NowFloder<>"" Then
%>
				<li><a href="Template.asp?Type=2&NowFloder=<%=NowFloder%>" target="dialog" width="700">新建文件</a></li>
<%
End If
%>
				<li><a href="Template.asp?Type=6"  target="delete" confirmStr="需要更新模板缓存么？" refreshUrl="<%=Request.Cookies("url_f02")%>" showStr="模板重载成功！">重载模板</a></li>
				<li><a href="Template.asp?Type=7" target="get" div="mainRight">模板缓存管理</a></li>
			</ul>
		</div>
		<div id="searchLine" class="font_bold">
			模板管理
		</div>
		<table width="100%" bordercolor="#CCCCCC" border="1" cellspacing="0" cellpadding="0">
			<tr>
				<th align="center">文件/文件夹名</th>
				<th align="center">类型</th>
				<th align="center">操作</th>
			</tr>
<%
	TempArr=Split(NowFloder,"/")
	For i=0 To UBound(TempArr)-1
		If DirFloder="" Then
			DirFloder=TempArr(i)
		Else
			DirFloder=DirFloder&"/"&TempArr(i)
		End If
	Next
	If NowFloder<>"" Then
%>
			<tr>
				<td height="20" colspan="3">&nbsp;&nbsp;&nbsp;&nbsp;<a href="Template.asp?Type=1&NowFloder=<%=DirFloder%>" title="返回上层" target="get" div="mainRight">../</a></td>
			</tr>
<%
	End If
	If NowFloder="" Then
		readFloder="../Skin/"
	Else
		NowFloder=NowFloder&"/"
		readFloder="../Skin/"&NowFloder
	End If
	TempArr=Split(FKFun.GetFloderList(readFloder),"||")
	For Each Temp In TempArr
%>
			<tr>
				<td height="20">&nbsp;&nbsp;<a href="Template.asp?Type=1&NowFloder=<%=NowFloder&Temp%>" target="get" div="mainRight"><%=Temp%></a></td>
				<td align="center">文件夹</td>
				<td align="center"><a href="Template.asp?Type=1&NowFloder=<%=NowFloder&Temp%>" target="get" div="mainRight">进入</a></td>
			</tr>
<%
	Next
	TempArr=Split(FKFun.GetFileList(readFloder),"||")
	For Each Temp In TempArr
		If Instr(",html,css,",LCase(Split(Temp,".")(UBound(Split(Temp,".")))))>0 Then
%>
			<tr>
				<td height="20">&nbsp;&nbsp;<a href="Template.asp?Type=4&File=<%=Temp%>&NowFloder=<%=NowFloder%>" target="dialog" width="700"><%=Temp%></a></td>
				<td align="center">.<%=UCase(Split(Temp,".")(UBound(Split(Temp,"."))))%></td>
				<td align="center"><a href="Template.asp?Type=4&File=<%=Temp%>&NowFloder=<%=NowFloder%>" target="dialog" width="700">编辑</a></td>
			</tr>
<%
		End If
	Next
%>
		</table>
		<div id="rightBottom">
		
		</div>
<%
End Sub

'==========================================
'函 数 名：TemplateAddForm()
'作    用：添加新文件表单
'==========================================
Sub TemplateAddForm()
	NowFloder=FKFun.HTMLEncode(Trim(Request.QueryString("NowFloder")))
%>
<form id="TemplateAdd" name="TemplateAdd" method="post" target="ajax" action="Template.asp?Type=3" showStr="新模板文件创建成功！" refreshUrl="<%=Session("NowPage")%>" boxClose="1">
<div id="title">创建新模板文件</div>
<div id="box">
	<table width="90%" border="1" bordercolor="#CCCCCC" align="center" cellpadding="0" cellspacing="0">
		<tr>
			<td width="14%" height="30" align="right">文件名：</td>
			<td width="86%">&nbsp;<span style="font-size:14px;">Skin/<%=NowFloder%>/</span>&nbsp;<input name="Fk_Template_Name" type="text" class="input" id="Fk_Template_Name" size="6" /><span style="font-size:14px;">.html</span><span class="help" title="<p>模板文件名，支持英文字符、数字、中文，请勿用特殊字符，长度请小于50个字符（两个字符为一个汉字）。</p>"></span></td>
		</tr>
		<tr>
			<td height="30" align="right">文件内容：</td>
			<td>&nbsp;<textarea name="Fk_Template_Content" rows="20" cols="60" class="textarea" id="Fk_Template_Content"></textarea></td>
		</tr>
	</table>
</div>
<div id="bottom">
	<input type="hidden" name="NowFloder" value="<%=NowFloder%>" />
	<input type="submit" class="button" name="Enter" id="Enter" value="创 建" />
	<input type="button" onclick="dialogClose();" class="button" name="button" id="button" value="关 闭" />
</div>
</form>
<%
End Sub

'==============================
'函 数 名：TemplateAddDo
'作    用：添加新文件执行
'==============================
Sub TemplateAddDo()
	NowFloder=FKFun.HTMLEncode(Trim(Request.Form("NowFloder")))
	Fk_Template_Name=FKFun.HTMLEncode(Trim(Request.Form("Fk_Template_Name")))
	Fk_Template_Content=Trim(Request.Form("Fk_Template_Content"))
	Call FKFun.JsonString(NowFloder,1,50,0,"模板文件夹内方可创建文件！","模板文件夹不能大于50个字符！")
	Call FKFun.JsonString(Fk_Template_Name,1,50,0,"请输入文件名！","文件名不能大于50个字符！")
	Call FKFun.JsonString(Fk_Template_Content,1,5000000,0,"请输入模板内容！","模板内容不能大于5000000个字符！")
	If NowFloder="" Then
		Call FKFun.JsonError("模板根目录下不允许新建文件！")
	End If
	If FKFun.IsFile("../Skin/"&NowFloder&"/"&Fk_Template_Name&".html") Then
		Call FKFun.JsonError("文件已经存在！")
	End If
	If NowFloder=Fk_Site_Template Then
		Sqlstr="Select Fk_Template_Id,Fk_Template_Name,Fk_Template_Content From [Fk_Template] Where Fk_Template_Name='"&Fk_Template_Name&"'"
		Rs.Open Sqlstr,Conn,1,3
		Application.Lock()
		If Rs.Eof Then
			Rs.AddNew()
			Rs("Fk_Template_Name")=Fk_Template_Name
			Rs("Fk_Template_Content")=Fk_Template_Content
			Rs.Update()
		Else
			Rs("Fk_Template_Content")=Fk_Template_Content
			Rs.Update()
		End If
		Application.UnLock()
		Rs.Close
	End If
	Call FKFun.CreateFile("../Skin/"&NowFloder&"/"&Fk_Template_Name&".html",Fk_Template_Content)
	Call FKFun.JsonSuccess()
End Sub

'==========================================
'函 数 名：TemplateEditForm()
'作    用：修改文件表单
'==========================================
Sub TemplateEditForm()
	NowFloder=FKFun.HTMLEncode(Trim(Request.QueryString("NowFloder")))
	NowFile=FKFun.HTMLEncode(Trim(Request.QueryString("File")))
	Fk_Template_Content=Server.HTMLEncode(FKFun.FsoFileRead("../Skin/"&NowFloder&NowFile))
%>
<form id="TemplateEdit" name="TemplateEdit" method="post" target="ajax" action="Template.asp?Type=5" showStr="模板修改成功！" refreshUrl="<%=Session("NowPage")%>" boxClose="1">
<div id="title">修改模板</div>
<div id="box">
	<table width="90%" border="1" bordercolor="#CCCCCC" align="center" cellpadding="0" cellspacing="0">
		<tr>
			<td width="14%" height="30" align="right">文件名：</td>
			<td width="86%">&nbsp;<span style="font-size:14px;">Skin/<%=NowFloder%><%=NowFile%></span></td>
		</tr>
		<tr>
			<td height="30" align="right">文件内容：</td>
			<td>&nbsp;<textarea name="Fk_Template_Content" rows="20" cols="60" class="textarea" id="Fk_Template_Content"><%=Fk_Template_Content%></textarea></td>
		</tr>
	</table>
</div>
<div id="bottom">
		<input type="hidden" name="NowFloder" value="<%=NowFloder%>" />
		<input type="hidden" name="NowFile" value="<%=NowFile%>" />
	<input type="submit" class="button" name="Enter" id="Enter" value="修 改" />
	<input type="button" onclick="dialogClose();" class="button" name="button" id="button" value="关 闭" />
</div>
</form>
<%
End Sub

'==============================
'函 数 名：TemplateEditDo
'作    用：执行修改模板
'==============================
Sub TemplateEditDo()
	NowFloder=FKFun.HTMLEncode(Trim(Request.Form("NowFloder")))
	NowFile=FKFun.HTMLEncode(Trim(Request.Form("NowFile")))
	Fk_Template_Content=Trim(Request.Form("Fk_Template_Content"))
	Call FKFun.JsonString(Fk_Template_Content,1,50000,0,"请输入模板内容！","模板内容不能大于50000个字符！")
	If Replace(NowFloder,"/","")=Fk_Site_Template And Instr(NowFile,".html")>0 Then
		Sqlstr="Select Fk_Template_Id,Fk_Template_Content From [Fk_Template] Where Fk_Template_Name='"&Replace(NowFile,".html","")&"'"
		Rs.Open Sqlstr,Conn,1,3
		Application.Lock()
		If Not Rs.Eof Then
			Rs("Fk_Template_Content")=Fk_Template_Content
			Rs.Update()
		End If
		Application.UnLock()
		Rs.Close
	End If
	Call FKFun.CreateFile("../Skin/"&NowFloder&NowFile,Fk_Template_Content)
	Call FKFun.JsonSuccess()
End Sub

'==============================
'函 数 名：TemplateReLoad
'作    用：模板缓存重新载入
'==============================
Sub TemplateReLoad()
	Dim dirTemp,dirTempArr,FileList
	FileList=FKFun.GetFileList("../Skin/"&Fk_Site_Template)
	
	dirTempArr=Split(FKFun.GetFloderList("../skin/"&Fk_Site_Template&"/"),"||")
	For Each dirTemp In dirTempArr
		If Left(dirTemp,2)="t_" Then
			Temp=Replace(FKFun.GetFileList("../Skin/"&Fk_Site_Template&"/"&dirTemp),"||","||"&dirTemp&"/")
			If Temp<>"" Then
				FileList=FileList&"||"&dirTemp&"/"&Temp
			End If
		End If
	Next
	
	TempArr=Split(FileList,"||")

	Conn.execute("DELETE FROM [Fk_Template]")
	Conn.execute("ALTER TABLE [Fk_Template] ALTER COLUMN Fk_Template_Id COUNTER (1,1);")
	
	For Each Temp In TempArr
		If LCase(Split(Temp,".")(UBound(Split(Temp,"."))))="html" Then
			Sqlstr="Select Fk_Template_Id,Fk_Template_Name,Fk_Template_Content From [Fk_Template] Where Fk_Template_Name='"&Replace(LCase(Temp),".html","")&"'"
			Rs.Open Sqlstr,Conn,1,3
			Application.Lock()
			If Not Rs.Eof Then
				Rs("Fk_Template_Content")=FKFun.FsoFileRead("../Skin/"&Fk_Site_Template&"/"&Temp)
				Rs.Update()
			Else
				Rs.AddNew()
				Rs("Fk_Template_Name")=LCase(Replace(LCase(Temp),".html",""))
				Rs("Fk_Template_Content")=FKFun.FsoFileRead("../Skin/"&Fk_Site_Template&"/"&Temp)
				Rs.Update()
			End If
			Application.Lock()
			Rs.Close
		End If
	Next
	Response.Write("ok")
End Sub

'==========================================
'函 数 名：TemplateTempList()
'作    用：模板缓存列表
'==========================================
Sub TemplateTempList()
%>
		<div id="rightNav">
			<ul>
				<li><a href="Template.asp?Type=1" target="get" div="mainRight">模板管理</a></li>
			</ul>
		</div>
		<div id="searchLine" class="font_bold">
			模板缓存管理
		</div>
		<table width="100%" bordercolor="#CCCCCC" border="1" cellspacing="0" cellpadding="0">
			<tr>
				<th align="center">序号</th>
				<th align="center">名称</th>
				<th align="center">操作</th>
			</tr>
<%
	Sqlstr="Select Fk_Template_Id,Fk_Template_Name From [Fk_Template] Order By Fk_Template_Id Desc"
	Rs.Open Sqlstr,Conn,1,1
	If Not Rs.Eof Then
		i=1
		While Not Rs.Eof
%>
			<tr>
				<td height="20" align="center"><%=i%></td>
				<td>&nbsp;<%=Rs("Fk_Template_Name")%></td>
				<td align="center"><a href="Template.asp?Type=8&Id=<%=Rs("Fk_Template_Id")%>" target="delete" confirmStr="您确认要删除“<%=Rs("Fk_Template_Name")%>”？此操作不可逆！" refreshUrl="Template.asp?Type=7" showStr="模板缓存删除成功！">删除</a></td>
			</tr>
<%
			Rs.MoveNext
			i=i+1
		Wend
	Else
%>
			<tr>
				<td height="25" colspan="20" align="center">暂无记录</td>
			</tr>
<%
	End If
	Rs.Close
%>
			<tr>
				<td height="30" colspan="20">&nbsp;</td>
			</tr>
		</table>
		<div id="rightBottom">
		
		</div>
<%
End Sub

'==========================================
'函 数 名：TemplateTempDel()
'作    用：模板缓存删除执行
'==========================================
Sub TemplateTempDel()
	Id=Trim(Request.QueryString("Id"))
	Call FKFun.ShowNum(Id,"系统参数错误，请刷新页面！")
	Sqlstr="Select * From [Fk_Template] Where Fk_Template_Id=" & Id
	Rs.Open Sqlstr,Conn,1,3
	If Not Rs.Eof Then
		Application.Lock()
		Rs.Delete()
		Application.UnLock()
		Response.Write("ok")
	Else
		Response.Write("广告不存在！")
	End If
	Rs.Close
End Sub

Call FKFun.PageEnd()
%>
